const express = require('express');
const router = express.Router();
const userController = require('../controllers/users.controller.js');
const { verifyToken } = require('../middleware/auth.middleware.js');
const multer = require('multer');

// 配置文件上传
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/uploads/avatars');
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
    cb(
      null,
      'avatar-' + uniqueSuffix + '.' + file.originalname.split('.').pop()
    );
  },
});
const upload = multer({ storage: storage });

// 公开路由
router.post('/register', userController.register);
router.post('/login', userController.login);

// 需要认证的路由
router.get('/profile', verifyToken, userController.getProfile);
router.put('/profile', verifyToken, userController.updateProfile);
router.put('/change-password', verifyToken, userController.changePassword);
router.post(
  '/avatar',
  verifyToken,
  upload.single('avatar'),
  userController.uploadAvatar
);
router.get('/favorites', verifyToken, userController.getFavorites);
router.get('/likes', verifyToken, userController.getLikes);

// 管理员路由
router.get('/', verifyToken, userController.getUsers);
router.get('/:id', verifyToken, userController.getUser);
router.delete('/:id', verifyToken, userController.deleteUser);

module.exports = router;
